//
//  MainSearchViewController.m
//  CyclePYSearch
//
//  Created by dihuijun on 17/7/10.
//  Copyright © 2017年 Cycle. All rights reserved.
//

#import "MainSearchViewController.h"



@interface MainSearchViewController ()<UISearchBarDelegate,UISearchControllerDelegate,UISearchResultsUpdating,PYSearchViewControllerDelegate>
/**   数据源 */
@property (nonatomic, strong) NSMutableArray *datasource;
@end

@implementation MainSearchViewController

/**数据源*/
-(NSMutableArray *)datasource{
    
    
    if(_datasource  == nil){
        _datasource           =         [NSMutableArray array];
        
                NSString *path        =         [[NSBundle mainBundle]pathForResource:@"mainData" ofType:@"json"];
        
                NSData   *data        =         [NSData dataWithContentsOfFile:path];
        
                NSError  *error       =         nil;
        
                NSDictionary *TotallyDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        
                NSArray *dictArray    =  TotallyDict[@"data"];
        
        
                if(error){
                    NSLog(@"出错啦:%@",error);
                }else{
                    for (NSDictionary *dict in dictArray) {
                        mainStatus *status = [mainStatus mainStatusWithDict:dict];
                        
                        [self.datasource addObject:status];
                        
                    }
//                    
//                    for (mainStatus *status in self.datasource) {
//                        NSLog(@"%@",status.username);
//                    }
                    
                }
    }
    return _datasource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.setupNav
    [self setupNav];
    
    
   
    
    
}
#pragma mark --- 1.setupNav
- (void)setupNav
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent  = NO;
    UIButton *titleButton           = [UIButton buttonWithType:UIButtonTypeCustom];
    titleButton.layer.cornerRadius  = 4.0;
    titleButton.layer.masksToBounds = YES;
    [titleButton addTarget:self action:@selector(ClickLeft) forControlEvents:UIControlEventTouchUpInside];
    [titleButton setImage:[UIImage imageNamed:@"search-main"] forState:UIControlStateNormal];
    [titleButton setImage:[UIImage imageNamed:@"search-main"] forState:UIControlStateHighlighted];
    
    titleButton.frame               = CGRectMake(0, 0, 300, 44);
    
    
    //    searchBar          = [[ZBSearchBar alloc]initWithFrame:CGRectMake(0, 0, 300, 30)];
    //    [searchBar addTarget:self action:@selector(ClickLeft) forControlEvents:UIControlEventEditingChanged];
    self.navigationItem.titleView   = titleButton;
}
#pragma mark --- 2.1 搜索
- (void)ClickLeft
{

    // 1.创建热门搜索
    NSArray *hotSeaches = @[@"智能口罩", @"手环/手表", @"智能眼镜"];
    // 2. 创建控制器
    PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:hotSeaches searchBarPlaceholder:@"搜索" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
        
        // 开始搜索执行以下代码
        // 如：跳转到指定控制器  temp
        
//        NSLog(@"%@",hotSeaches);
        NSLog(@"searchText: %@",searchText);
 
        
        
        //   PYTempViewController *tempVC = [[PYTempViewController alloc] init];
        //tempVC.selectedContent = searchText;
        // [searchViewController.navigationController pushViewController:tempVC animated:YES];
    }];
    // 3. 设置风格
    // if (indexPath.section == 0) { // 选择热门搜索
    //   searchViewController.hotSearchStyle = (NSInteger)indexPath.row; // 热门搜索风格根据选择
    searchViewController.searchHistoryStyle = PYHotSearchStyleDefault; // 搜索历史风格为default
    // } else { // 选择搜索历史
    searchViewController.hotSearchStyle = PYHotSearchStyleARCBorderTag; // 热门搜索风格为默认
    // searchViewController.searchHistoryStyle = (NSInteger)indexPath.row; // 搜索历史风格根据选择
    //}
    // 4. 设置代理
    searchViewController.delegate = self;
    // 5. 跳转到搜索控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchViewController];
    [self presentViewController:nav  animated:NO completion:nil];
}
#pragma mark - 2.2 PYSearchViewControllerDelegate(搜索框:输入内容会来到这里)
- (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText
{
    
    NSLog(@"%@",searchText);
    if (searchText.length) { // 与搜索条件再搜索
          // 显示建议搜索结果
        NSMutableArray *searchSuggestionsM = [NSMutableArray array];
        
        for (mainStatus *status in self.datasource) {
            if ([status.username containsString:searchText]) {
                [searchSuggestionsM addObject:status.username];
            }
        }
        
        // 返回
        searchViewController.searchSuggestions = searchSuggestionsM;
        
        
    }
}



@end
